#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;
using fsp = fs::perms;

int main()
{
    {
        std::cout << "1- " << fs::read_symlink("/proc/self/exe").c_str() << std::endl;
    }
    // 1- /home/luwh/repo/demos/build/out/cpp.standard_library.filesystem

    {
        fs::directory_entry entry(".");
        if (entry.status().type() == fs::file_type::directory) {
            fs::directory_iterator files(".");
            for (const auto &file : files) {
                std::cout << "2- " << file.path().c_str() << std::endl;
            }
        }
    }
    // 2- ./LICENSE
    // 2- ./.gitignore
    // 2- ./Makefile
    // 2- ./compile_commands.json
    // 2- ./C++
    // 2- ./plantuml
    // 2- ./LeetCode
    // 2- ./interfaces
    // 2- ./third_party_demo
    // 2- ./out
    // 2- ./BUILD.gn
    // 2- ./build
    // 2- ./ACM
    // 2- ./projects
    // 2- ./README.md
    // 2- ./shell
    // 2- ./.git
    // 2- ./qemu
    // 2- ./third_party
    // 2- ./.gn
    // 2- ./.build

    {
        std::cout << "3- " << std::boolalpha << fs::exists("filesystem") << std::endl;
    }
    // 3- false

    {
        std::error_code ec;
        fs::create_directory("filesystem", ec);
        fs::permissions("filesystem", fsp::owner_all | fsp::group_read | fsp::group_exec | fsp::others_read | fsp::others_exec, fs::perm_options::replace);
        std::cout << "4- " << ec.message() << std::endl;
    }
    // 4- Success

    {
        std::error_code ec;
        std::cout << "5- " << std::boolalpha << fs::exists("filesystem", ec) << std::endl;
        std::cout << "5- " << ec.message() << std::endl;
    }
    // 5- true
    // 5- Success

    {
        std::error_code ec;
        std::cout << "6- " << std::boolalpha << fs::remove("filesystem", ec) << std::endl;
        std::cout << "6- " << ec.message() << std::endl;
    }
    // 6- true
    // 6- Success

    {
        std::error_code ec;
        std::cout << "7- " << std::boolalpha << fs::exists("filesystem", ec) << std::endl;
        std::cout << "7- " << ec.message() << std::endl;
    }
    // 7- false
    // 7- Success

    {
        std::error_code ec;
        std::cout << "8- " << std::boolalpha << fs::exists("/root/.bashrc", ec) << std::endl;
        std::cout << "8- " << ec.message() << std::endl;
    }
    // 8- false
    // 8- Permission denied
    return 0;
}
